-
-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for the driver.Connector interface. #41
Conversation
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
"io" | ||
"sync" | ||
) | ||
|
||
// New returns a driver.Connector, which can be passed to sql.OpenDB. This can | ||
// be used in place of Register. | ||
func New(drv, dsn string, options ...func(*conn) error) driver.Connector { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if New
is the best function name for this. I'm open to alternative suggestions.
097559b
to
6d2e6f3
Compare
Hi it is very important what dsn is used for txdb Also, we cannot just increment the number there, since in some cases it is rather important to reuse same connection. since in applications it sometimes uses same database at the same time but with different connections. So it must be possible to specify the dsn in a clean way and reuse or create a separate transaction based on it. |
Based on my reading of the code, that dsn is only used as a map in the *txDriver object, and not globally. So the way I envision enabling separate transactions here is by making separate calls to txn1, err := sql.OpenDB(txdb.New("mysql", "root@/txdb_test"))
/* ... */
txn2, err := sql.OpenDB(txdb.New("mysql", "root@/txdb_test")) This should be equivalent to the old way: txdb.Register("txdb", "mysql", "root@/txdb_test")
/* ... */
txn1, err := sql.Open("txdb", "unique_string_1")
/* ... */
txn2, err := sql.Open("txdb", "unique_string_2") Or maybe there's some implication of the dsn I'm not seeing immediately? |
txdb.Register in go, creates txdriver instance only once. Every time db.open is called with dsn, the driver looks into the dsn to transaction connection map based on that dsn it either opens a new transaction or reuses the transaction which is already open (in this case every action to that transaction is protected with mutex) |
Are you saying there's a difference in behavior between these two approaches? I don't see any difference. It's already possible, using the Register method, to create multiple simultaneous transactions, by providing different dsns. This is no different than creating multiple Connector instances with New. (It's also no different than registering multiple txdb drivers to the same db, which is also possible today). The only difference I see is where the connection disambiguation occurs. With the Register approach, the Logically, I believe these are identical. The mutexes I see only protect the members of |
@flimzy would you be open to recover this? It seems that |
@l3pp4rd Any objection to me updating this PR to get it merged? |
That was fast! Truly appreciated. |
@lopezator This project has seemingly gone without a lot of attention for quite a while. I'm sure this PR just got lost when attention was focused on other tasks at DATA-DOG. Hopefully we can get this ball rolling again. |
aee93f2
to
086c570
Compare
@flimzy Looks good, would be better if adding some unit tests, or refactoring current tests to also init db using |
@Yiling-J Good suggestion. I've added a very simple test to exercise just the new code. WDYT? |
Should we take the opportunity to implement the |
I agree on both counts. It sounds like something we should definitely support, and it should be a separate PR. |
Fixes #40